package com.laowang.project.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.github.pagehelper.PageHelper;
import com.laowang.common.exception.CustomException;
import com.laowang.common.utils.SecurityUtils;
import com.laowang.common.utils.StringUtils;
import com.laowang.framework.web.page.TableDataInfoMy;
import com.laowang.project.system.domain.MyCyhShop;
import com.laowang.project.system.domain.SysUser;
import com.laowang.project.system.service.ISysUserService;
import com.laowang.project.system.service.MyCyhShopCateService;
import com.laowang.project.system.service.MyCyhShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.laowang.framework.aspectj.lang.annotation.Log;
import com.laowang.framework.aspectj.lang.enums.BusinessType;

import org.springframework.web.bind.annotation.*;
import com.laowang.project.system.domain.MyCoupon;
import com.laowang.project.system.service.MyCouponService;
import com.laowang.framework.web.controller.BaseController;
import com.laowang.framework.web.domain.AjaxResult;
import com.laowang.common.utils.poi.ExcelUtil;
import com.laowang.framework.web.page.TableDataInfo;

/**
 * 用户购买的优惠券Controller
 *
 * @author laowang
 * @date 2023-07-25
 */
@Api(tags = "用户购买的优惠券接口")
@RestController
@RequestMapping("/myCoupon")
public class MyCouponController extends BaseController {
    @Autowired
    private MyCouponService myCouponService;

    @Autowired
    private MyCyhShopService myCyhShopService;



    /**
     * 查询用户购买的优惠券列表
     */
    @ApiOperation(value = "小程序端 查询用户购买的优惠券",notes = "")
    @GetMapping("/web/list")
    public TableDataInfoMy weblist(MyCoupon myCoupon) {

        PageHelper.startPage(myCoupon.getPageNum(),myCoupon.getPageSize());
        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();

        myCoupon.setUserId(mid);
        List<MyCoupon> list = myCouponService.selectMyCouponListWeb(myCoupon);
        return getDataTableMy(list);
    }

    /**
     * 查询用户购买的优惠券列表
     */
    @ApiOperation(value = "小程序端 商家核销",notes = "")
    @GetMapping("/check")
    public AjaxResult check(Integer num) {
        MyCoupon myCoupon = myCouponService.selectMyCouponById(num);
        if (myCoupon == null){
            return AjaxResult.error("优惠券码不存在");
        }

        if (myCoupon.getExpireTime().before(new Date())){
            return AjaxResult.error("优惠券码已过期");
        }

        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        MyCyhShop myCyhShop = new MyCyhShop();
        myCyhShop.setUserId(mid);
        List<MyCyhShop> myCyhShops = myCyhShopService.selectMyCyhShopListWeb(myCyhShop);
        Boolean flag =false;
        for (MyCyhShop cyhShop : myCyhShops) {
            if (cyhShop.getId().equals(myCoupon.getShopId())){
                flag = true;
            }
        }
        if (!flag){
            return AjaxResult.error("不能核销其他商家的券");
        }
        myCoupon.setIsCheck(1);
        myCoupon.setCheckTime(new Date());
        myCouponService.updateMyCoupon(myCoupon);
        return AjaxResult.success("操作成功");
    }

    @ApiOperation(value = "小程序端 商家核销记录",notes = "")
    @GetMapping("/checkRecord")
    public AjaxResult checkRecord() {

        Integer mid = SecurityUtils.getLoginUser().getWxUser().getMid();
        MyCyhShop myCyhShop = new MyCyhShop();
        myCyhShop.setUserId(mid);
        List<MyCyhShop> myCyhShops = myCyhShopService.selectMyCyhShopListWeb(myCyhShop);
        List<Integer> collect = myCyhShops.stream().map(myCyhShop1 -> myCyhShop1.getId()).collect(Collectors.toList());
        List<MyCoupon> couponList = new ArrayList<>();
        if (collect.size()> 0){
            couponList =  myCouponService.checkRecord(collect);
        }
        return AjaxResult.success("操作成功",couponList);
    }



    /**
     * 查询用户购买的优惠券列表
     */
    @ApiOperation(value = "查询用户购买的优惠券列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:list')")
    @GetMapping("/list")
    public TableDataInfo list(MyCoupon myCoupon) {

        startPage();
        List<MyCoupon> list = myCouponService.selectMyCouponList(myCoupon);
        return getDataTable(list);
    }

    /**
     * 导出用户购买的优惠券列表
     */
    @ApiOperation(value = "导出用户购买的优惠券列表",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:export')")
    @Log(title = "用户购买的优惠券", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MyCoupon myCoupon) {
        List<MyCoupon> list = myCouponService.selectMyCouponList(myCoupon);
        ExcelUtil<MyCoupon> util = new ExcelUtil<MyCoupon>(MyCoupon.class);
        return util.exportExcel(list, "myCoupon");
    }

    /**
     * 获取用户购买的优惠券详细信息
     */
    @ApiOperation(value = "获取用户购买的优惠券详细信息",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(myCouponService.selectMyCouponById(id));
    }

    /**
     * 新增用户购买的优惠券
     */
    @ApiOperation(value = "新增用户购买的优惠券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:add')")
    @Log(title = "用户购买的优惠券", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MyCoupon myCoupon) {
        return toAjax(myCouponService.insertMyCoupon(myCoupon));
    }

    /**
     * 修改用户购买的优惠券
     */
    @ApiOperation(value = "修改用户购买的优惠券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:edit')")
    @Log(title = "用户购买的优惠券", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MyCoupon myCoupon) {
        return toAjax(myCouponService.updateMyCoupon(myCoupon));
    }

    /**
     * 删除用户购买的优惠券
     */
    @ApiOperation(value = "删除用户购买的优惠券",notes = "")
    @PreAuthorize("@ss.hasPermi('my:shopCoupon:remove')")
    @Log(title = "用户购买的优惠券", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(myCouponService.deleteMyCouponByIds(ids));
    }
}
